package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接消息服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);//默认的端口可以省略
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c=f.newConnection().createChannel();//连接

        //定义fanout类型交换机:logs
        //1.命名字  2.类型
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);


        // 向logs交换机发送消息
        while (true) {
        System.out.println("输入消息");
        String s=new Scanner(System.in).nextLine();
        /*
        * 参数
        * 1.交换机名字
        * 2.对于fanout交换机无效
        * 3.其他属性
        *发送的数据
        * */
        c.basicPublish("logs", "", null, s.getBytes());
    }

}
}
